package org.example.sword2offer.primary;

/**
 * @author: lynn
 * @Descript: 判断是否为对称的二叉树 @link Q18_MirrorTrees
 * 请实现一个函数，用来判断一颗二叉树是不是对称的。注意，如果一个二叉树同此二叉树的镜像是同样的，定义其为对称的。
 * 解题思路: https://blog.csdn.net/gaoyueace/article/details/89556207
 * @date: 2021/3/24 20:06
 * @version: 1.0
 */
public class Q57_IsSymmetricalTree {

    public static void main(String[] args) {

    }
    public static boolean isSymmetrical(TreeNode pRoot) {
        if (pRoot==null){
            return true;
        }
        return comValue(pRoot.left,pRoot.right);
    }


    public static boolean comValue(TreeNode nodeLeft, TreeNode nodeRight) {
        if (nodeLeft==null) return nodeRight==null;
        if (nodeRight==null) return  false;
        if (nodeLeft.val !=nodeRight.val) return false;
        //比较方式不好理解
        return comValue(nodeLeft.left,nodeRight.right) && comValue(nodeRight.left,nodeLeft.right);
    }

    private  static  class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val) {
            this.val=val;
        }
    }
}
